﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class create_event_notification : BaseForm
    {
        public create_event_notification()
            : base()
        {
        }

        public create_event_notification(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbQueue.DataSource = Utility.queues(dbContext);
            cbQueue.DisplayMember = "Name";

            cbEvent.DataSource = Utility.notifications(dbContext);
            cbEvent.DisplayMember = "Name";

        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("CREATE EVENT NOTIFICATION [" + tbName.Text + "]");

            if (rbDatabase.Checked) sb.AppendLine("ON DATABASE");
            if (rbServer.Checked) sb.AppendLine("ON SERVER");
            if (rbQueue.Checked)
            {
                if (cbQueue.SelectedIndex > 0)
                    sb.AppendLine("ON QUEUE [" + cbQueue.Text + "]");
            }

            if (cbFanin.Checked)
                sb.AppendLine("WITH FAN_IN");

            sb.AppendLine("FOR");

            if (lbEventList.Items.Count > 0)
            {
                bool colon = false;

                foreach (string item in lbEventList.Items)
                {
                    if (colon)
                    {
                        sb.Append("   ,");
                    }
                    else
                    {
                        sb.Append("    ");
                    }
                    sb.Append(item);
                    sb.AppendLine();
                    colon = true;
                }
            }

            sb.Append("TO SERVICE '" + tbServiceName.Text + "'");
            if (tbInstance.Text != string.Empty) sb.Append(", '" + tbInstance.Text + "'");
            if (cbCurrent.Checked) sb.Append(", 'current database'");

            tbCommand.Text = sb.ToString();
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void rbServer_CheckedChanged(object sender, EventArgs e)
        {
            Write();

        }

        private void rbDatabase_CheckedChanged(object sender, EventArgs e)
        {
            Write();

        }

        private void rbQueue_CheckedChanged(object sender, EventArgs e)
        {
            if (rbQueue.Checked)
            {
                cbQueue.Enabled = true;
                lblQueue.Enabled = true;

                rbGroup.Enabled = false;
                rbType.Checked = true;

                cbEvent.DataSource = Utility.broker_notifications(dbContext);
                cbEvent.DisplayMember = "Name";
            }
            else
            {
                cbQueue.Enabled = false;
                lblQueue.Enabled = false;
                rbGroup.Enabled = true;

                if (rbType.Checked)
                {
                    cbEvent.DataSource = Utility.notifications(dbContext);
                    cbEvent.DisplayMember = "Name";
                }
                else
                {
                    cbEvent.DataSource = Utility.event_group();
                    cbEvent.DisplayMember = "Name";
                }
            }

            lbEventList.Items.Clear();

            Write();
        }

        private void cbQueue_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();

        }

        private void rbType_CheckedChanged(object sender, EventArgs e)
        {
            if (rbType.Checked)
            {
                if (!rbQueue.Checked)
                {
                    cbEvent.DataSource = Utility.notifications(dbContext);
                    cbEvent.DisplayMember = "Name";
                }
                else
                {
                    cbEvent.DataSource = Utility.broker_notifications(dbContext);
                    cbEvent.DisplayMember = "Name";
                }
            }
            Write();
        }

        private void rbGroup_CheckedChanged(object sender, EventArgs e)
        {
            if (rbGroup.Checked)
            {
                cbEvent.DataSource = Utility.event_group();
                cbEvent.DisplayMember = "Name";
            }
            Write();
        }

        private void cbEvent_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (cbEvent.SelectedIndex > 0)
            {
                lbEventList.Items.Add(cbEvent.Text);
            }

            Write();

        }

        private void lbEventList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();

        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lbEventList.SelectedIndex > 0)
                lbEventList.Items.Remove(lbEventList.SelectedIndex);

            Write();

        }

        private void tbServiceName_TextChanged(object sender, EventArgs e)
        {
            Write();

        }

        private void rbCurrent_CheckedChanged(object sender, EventArgs e)
        {
            if (cbCurrent.Checked)
            {
                lblInstance.Enabled = false;
                tbInstance.Text = "";
                tbInstance.Enabled = false;
            }
            else
            {
                lblInstance.Enabled = true;
                tbInstance.Enabled = true;
            }
            Write();
        }

        private void tbInstance_TextChanged(object sender, EventArgs e)
        {
            Write();

        }

        private void cbFanin_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }
    }
}